package Lee_code;

import java.util.List;

/**
 * @author blh
 * @date 2024年06月28日 16:07
 */
public class Lee_2_628 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        // cur指向当前节点
        ListNode cur = new ListNode(-1);
        ListNode temp = cur;
        int jiayi = 0;
        while (l1 != null || l2 != null) {
            ListNode next;
            if (l1 != null && l2 != null) {
                // 二者都有值
                int value = l1.val + l2.val + jiayi;
                // 取个位数
                if (value >= 10) {
                    value = value - 10;
                    jiayi = 1;
                } else {
                    jiayi = 0;
                }
                next = new ListNode(value);
                cur.next = next;
                l1 = l1.next;
                l2 = l2.next;
                cur = cur.next;
            } else if (l1 == null && l2 != null) {
                int value = l2.val + jiayi;
                if (value >= 10) {
                    value = value - 10;
                    jiayi = 1;
                } else {
                    jiayi = 0;
                }
                next = new ListNode(value);
                cur.next = next;
                cur = cur.next;
                l2 = l2.next;
            } else if (l2 == null && l1 != null) {
                int value = l1.val + jiayi;
                if (value >= 10) {
                    value = value - 10;
                    jiayi = 1;
                } else {
                    jiayi = 0;
                }
                next = new ListNode(value);
                cur.next = next;
                cur = cur.next;
                l1 = l1.next;
            }
            if (l1==null && l2==null && jiayi==1){
                cur.next = new ListNode(1);
                break;
            }
        }
        return temp.next;
    }
}
